import React from 'react'
import { Link } from 'react-router-dom'

const isMiniprogram = 'undefined' != typeof wx && wx.getSystemInfoSync

export function navigateTo(history, url) {
  if (!process.env.isMiniprogram) {
  } else {
  }
}

export default class extends React.Component {
  onClickLink = event => {
    if (this.props.onClick) {
      this.props.onClick(event)
    }

    if ('undefined' != typeof wx && wx.getSystemInfoSync) {
      event.defaultPrevented = true
      if (!this.props.to.startsWith('/')) {
        throw new Error('Link need absolute url.')
      }
      wx.navigateTo({ url: this.props.to })
    }
  }

  render() {
    const { onClick, ...rest } = this.props
    return <Link onClick={this.onClickLink} {...rest} />
  }
}
